package com.sr.servlet.book;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sr.entity.Book;
import com.sr.service.BookService;

/**
 * 实现最终修改功能的Servlet
 */
/**
 * Servlet implementation class ProductEditServlet2
 */
@WebServlet("/editBook2")
public class BookEditServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookEditServlet2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取修改页面的请求参数，然后调用业务逻辑层对象，实现最终修改
				request.setCharacterEncoding("UTF-8");
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html; charset=UTF-8");
				int bid=Integer.parseInt(request.getParameter("bId"));
				String bookName=request.getParameter("bookName");
				String author=request.getParameter("author");
				String img=request.getParameter("img");
				Book b=new Book(bid,bookName,author,img);
				BookService bs=new BookService();
				boolean result=bs.updateBookById(b);
				System.out.println(b);
				if(result){
					request.getRequestDispatcher("findBooklist").forward(request, response);
				}else{//否则再次跳回修改页面 重新修改
					response.sendRedirect("editBook.jsp");
				}
	}

}
